#pragma once
#include <unordered_map>
#include <google/protobuf/service.h>
#include "tinyrpc/net/tcpserver.h"
#include "RedisRegister.h"
#include "tinyrpc/base/conf/config.h"
namespace TinyRPC{
struct ServiceInfo{
    google::protobuf::Service* m_service;
    std::unordered_map<std::string, const google::protobuf::MethodDescriptor*> m_methodMap;
};

class TinyRPCServer : public TcpServer{
public:
    TinyRPCServer() : TcpServer(&m_eventLoop, 0){}
    void init(const std::string &filePath);
    void registService(google::protobuf::Service * service);
    void start();
private:
    void accept();
    void handleRPCConnection(SocketChannelPtr socketChannel);
    void sendRpcResponse(SocketChannelPtr socketChannel, google::protobuf::Message * response);
private:
    Config m_config;
    std::unordered_map<std::string, ServiceInfo> m_serviceMap;
    RedisRegister m_register;
    EventLoop m_eventLoop;
    INetAddress m_serverAddress;
};
}
